<?php
	class guidenphongbandb
	{
		public function Add($MaVanBanDen, $MaPhongBan)
		{
			$sql = " INSERT INTO `tbl_guidenphongban`(`MaVanBanDen`, `MaPhongBan`) VALUES ('$MaVanBanDen', $MaPhong )";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Delete($MaVanBanDen)
		{
			$sql = " DELETE FROM `tbl_guidenphongban` WHERE MaVanBanDen = '$MaVanBanDen' ";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		
		public function GetList($MaVanBanDen, $SovaKyHieu, $NgayBanHanh, $NgayHetHan, $TrichYeu, $MaLoaiVanBan, $MaTrangThai, $NgayDen, $ThoiGianDuyet, $NoiBanHanh)
		{
			$sql = " SELECT * FROM `tbl_guidenphongban` vb, `tbl_loaivanban` lvb, `tbl_trangthai` tt WHERE  vb.MaLoaiVanBan = lvb.MaLoaiVanBan AND vb.MaTrangThai = tt.MaTrangThai ";
			if($MaVanBanDen != "")
				$sql .= " AND vb.MaVanBanDen = '$MaVanBanDen' ";
			if($SovaKyHieu != "")
				$sql .= " AND vb.SovaKyHieu LIKE '%$SovaKyHieu%'";
			if($NgayBanHanh != "")
				$sql .= " AND vb.NgayBanHanh = STR_TO_DATE('$NgayBanHanh','%d,%m,%Y')";
			if($NgayHetHan != "")
				$sql .= " AND vb.NgayHetHan = STR_TO_DATE('$NgayHetHan','%d,%m,%Y')";
			if($TrichYeu != "")
				$sql .= " AND vb.TrichYeu LIKE '%$TrichYeu%'";
			if($MaLoaiVanBan != "")
				$sql .= " AND vb.MaLoaiVanBan = $MaLoaiVanBan";
			if($MaTrangThai != "")
				$sql .= " AND vb.MaTrangThai = $MaTrangThai";
			if($NgayDen != "")
				$sql .= " AND vb.NgayDen = STR_TO_DATE('$NgayDen','%d,%m,%Y')";
			if($ThoiGianDuyet != "")
				$sql .= " AND vb.ThoiGianDuyet = STR_TO_DATE('$ThoiGianDuyet','%d,%m,%Y')";
			if($NoiBanHanh != "")
				$sql .= " AND vb.NoiBanHanh LIKE '%$NoiBanHanh%'";	
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		
		public function GetVanBanCanDuyet($MaVanBanDen, $SovaKyHieu, $NgayBanHanh, $NgayHetHan, $TrichYeu, $NgayDen, $ThoiGianDuyet, $NoiBanHanh)
		{
			$sql = " SELECT * FROM `tbl_guidenphongban` vb, `tbl_loaivanban` lvb, `tbl_trangthai` tt WHERE  vb.MaLoaiVanBan = lvb.MaLoaiVanBan AND vb.MaTrangThai = tt.MaTrangThai AND vb.ThoiGianDuyet > CURDATE() AND vb.MaNguoiDuyet = '' ";
			
			if($MaVanBanDen != "")
				$sql .= " AND vb.MaVanBanDen = '$MaVanBanDen' ";
			if($SovaKyHieu != "")
				$sql .= " AND vb.SovaKyHieu LIKE '%$SovaKyHieu%'";
			if($NgayBanHanh != "")
				$sql .= " AND vb.NgayBanHanh = STR_TO_DATE('$NgayBanHanh','%d,%m,%Y')";
			if($NgayHetHan != "")
				$sql .= " AND vb.NgayHetHan = STR_TO_DATE('$NgayHetHan','%d,%m,%Y')";
			if($TrichYeu != "")
				$sql .= " AND vb.TrichYeu LIKE '%$TrichYeu%'";
			if($NgayDen != "")
				$sql .= " AND vb.NgayDen = STR_TO_DATE('$NgayDen','%d,%m,%Y')";
			if($ThoiGianDuyet != "")
				$sql .= " AND vb.ThoiGianDuyet = STR_TO_DATE('$ThoiGianDuyet','%d,%m,%Y')";
			if($NoiBanHanh != "")
				$sql .= " AND vb.NoiBanHanh LIKE '%$NoiBanHanh%'";	
	
				
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		
		public function CheckDuyet($MaVanBanDen, $MaNguoiDuyet)
		{
			$sql = " UPDATE `tbl_guidenphongban` SET 
			`MaNguoiDuyet` = $MaNguoiDuyet
			WHERE `MaVanBanDen`= '$MaVanBanDen' ";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
	}
?>